package com.haojiang.mh.remote.ui.frag

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.haojiang.mh.R
import com.haojiang.mh.remote.adapter.AlarmNewFragAdapter
import com.haojiang.mh.remote.base.BaseFrag
import com.haojiang.mh.remote.enums.EArea
import com.haojiang.mh.remote.event.FunChangeEvent
import com.haojiang.mh.remote.event.ReceiverEvent
import com.haojiang.mh.remote.ui.dialog.AlarmNewAddDialogFrag
import com.haojiang.mh.remote.utils.BleUtil.Companion.isDevConncted
import com.haojiang.mh.remote.utils.BleUtil.Companion.lastConnect
import com.haojiang.mh.remote.utils.Util.Companion.playSound
import kotlinx.android.synthetic.main.frag_alarm_new.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


class AlarmNewFrag : BaseFrag() {

    val alarmNewFragAdapter by lazy { AlarmNewFragAdapter() }

    private val onClickListener = object : View.OnClickListener {
        override fun onClick(v: View?) {
            playSound()
            if (!isDevConncted()) {
                showToast("请先连接设备再执行此操作")
                return
            }
            when (v!!.id) {
                R.id.tvAdd -> {
                    AlarmNewAddDialogFrag.show(activity!!)
                }
                R.id.tvDel -> {

                }
                else -> {
                }
            }
        }
    }

    override fun onLayoutId(): Int {
        return R.layout.frag_alarm_new
    }

    override fun initView() {
//        LogUtil.e("闹钟视图被加载")
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
//        return View.inflate(context, R.layout.frag_alarm_new, null)
    }


    override fun initListener() {
        rvAlarm.layoutManager = LinearLayoutManager(context)
        rvAlarm.adapter = alarmNewFragAdapter


        tvAdd.setOnClickListener(onClickListener)
        tvDel.setOnClickListener(onClickListener)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onReceiverEvent(event: ReceiverEvent) {
//        when (event.rxData) {
//            RX_ALARM_SET_SUCCESS -> {
//                //保存闹钟功能数据
////                lastConnect.bleDeviceList.forEach {
////                    SpUtil.putData(context!!, SECTION_ALARM, it.bleAddress, cmdCode)
////                }
//                showToast("设置成功")
//            }
//            RX_ALARM_CANCEL_SUCCESS -> {
//                showToast("闹钟取消成功")
//            }
//        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onFunctionEvent(event: FunChangeEvent) {
        when (event.fragCode) {
            EArea.ALARM.key -> {
                lastConnect.surface?.alarmList?.let {
                    if (it.size > 0 && !lastConnect.isAlarmInit) {
                        lastConnect.isAlarmInit = true
                        showFrag()
                    }
                }
            }
        }
    }

    //闹钟数据回传
    private fun showFrag() {
//        if (alarmList.size > 0) {
//            alarmMemFragAdapter.setData(alarmList)
//            alarmMsgFragAdapter.setData(alarmList)
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                timePicker.hour = Calendar.getInstance()[Calendar.HOUR_OF_DAY]
//                mHour = timePicker.hour
//                timePicker.minute = Calendar.getInstance()[Calendar.MINUTE]
//                mMinute = timePicker.minute
//            }
//        }
    }


    override fun initDestroy() {
        EventBus.getDefault().unregister(this)
    }





}